package com.jy.controller;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jy.common.ResultInfo;
import com.jy.entity.Carport;
import com.jy.service.ICarportService;
import com.jy.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 车位管理表 前端控制器
 * </p>
 *
 * @author xiexingxin
 * @since 2022-11-15
 */
@RestController
@RequestMapping("/carport")
@Slf4j
public class CarportController {

    @Autowired
    private ICarportService iCarportService;

    @GetMapping("/getcarport-info")
    public Result getcarportInfo(String carportName) {
        System.out.println(carportName);
        List<Carport> carports = iCarportService.getcarportInfo(carportName);
        log.info("查询到的车位信息数量为：{}条！", carports.size());
        return carports.size() > 0 ? Result.success(carports) : Result.failed(ResultInfo.SELECT_FAIL);
    }

    @GetMapping("/getcarportAll-info")
    public Result getcarportAllInfo() {
        List<Carport> list = iCarportService.list();
        log.info("查询到的车位详细信息数量为：{}条！", list.size());
        return list.size() > 0 ? Result.success(list) : Result.failed();
    }

    @GetMapping("/getcarportWithperosonInfoById")
    public Result getcarportWithperosonInfoById(Integer carportId) {
        log.debug("获取到的carportId：{}", carportId);
        Map map = iCarportService.getcarportWithperosonInfoById(carportId);
        return map.size() > 0 ? Result.success(map) : Result.failed(ResultInfo.SELECT_FAIL);
    }

    /**
     * 添加车位
     *
     * @param carport
     * @return
     */
    @PostMapping("/addcarport-info")
    public Result addCarportInfo(@RequestBody Carport carport) {
        if (carport.getStatu() != 1 || carport.getStatu() != 2) {
            return Result.failed(ResultInfo.POST_STATU_IS_ONEORTWO);
        }
        boolean save = iCarportService.save(carport);
        log.debug("获取到的carport数据：{}", carport);
        if (save) {
            return Result.success(ResultInfo.ADD_SUCCESS, save);
        }
        return Result.failed(ResultInfo.ADD_FAIL);
    }

    /**
     * 根据ID删除某个车位信息
     *
     * @param id
     * @return
     */
    @GetMapping("/delcarportById")
    public Result delCarportById(Integer id) {
        boolean b = iCarportService.removeById(id);
        log.debug("获取到的carportId：{}", id);
        if (b) {
            return Result.success(ResultInfo.DELETE_SUCCESS, b);
        }
        return Result.failed(ResultInfo.DELETE_FAIL);
    }

    /**
     * 根据Id修改车位信息
     *
     * @param carport
     * @return
     */
    @PostMapping("/updcarportById")
    public Result updCarportById(@RequestBody Carport carport) {
        if (carport.getCarportName().trim().isEmpty()) {
            return Result.failed(ResultInfo.POST_NAME_ISNOTNULL);
        }
        boolean b = iCarportService.updateById(carport);
        return b == true ? Result.success(ResultInfo.UPDATE_SUCCESS, b) : Result.failed(ResultInfo.UPDATE_FAIL);
    }

    /**
     * 根据Id修改车位销售状态
     *
     * @param carport
     * @return
     */
    @PostMapping("/updcarportByStatu")
    public Result updCarportByStatu(@RequestBody Carport carport) {
        UpdateWrapper updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", carport.getId());
        updateWrapper.set("statu", carport.getStatu());
        boolean update = iCarportService.update(updateWrapper);
        return update == true ? Result.success(ResultInfo.UPDATE_SUCCESS, update) : Result.failed(ResultInfo.UPDATE_FAIL);
    }

}
